﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace LxCFD
{
    public class FluentMesh : FluentMeshBase, IReadFile
    {
        public StreamReader Reader;
        public char[] SplitChar = new char[] { ' ', '(', ')' };
        public int LineNum = 0;
        public string LineContent;
        public string[] SplitedLine;
        public void ReadLine()
        {
            LineContent = Reader.ReadLine();
            LineNum++;
            SplitedLine = LineContent.Split(SplitChar, StringSplitOptions.RemoveEmptyEntries);
        }
        public bool ReadFile(string filePath)
        {
            if (!ReadMeshFile(filePath)) return false;
            if (!ConvertToMesh()) return false;
            return true;
        }

        public bool ReadMeshFile(string filePath)
        {
            LxConsole.Info("开始读取网格……");
            Reader = new StreamReader(filePath, Encoding.Default, false);

            while (!Reader.EndOfStream)
            {
                 ReadLine();
                if (LineContent.StartsWith("(10"))//点
                {
                    if (SplitedLine[1] != "0")
                    {
                        FlHead mp = new FlHead();
                        mp.SetHead(SplitedLine);
                        FlNodes = new Point3D[mp.Index.GetCount()];
                        if (!LineContent.EndsWith("("))
                        {
                            ReadLine();
                        }
                        for (int i = 0; i < FlNodes.Length; i++)
                        {
                            ReadLine();
                            FlNodes[i] = new Point3D(double.Parse(SplitedLine[0]), double.Parse(SplitedLine[1]), double.Parse(SplitedLine[2]));
                        }
                    }
                }
                else if (LineContent.StartsWith("(12"))//zone
                {
                    if (SplitedLine[1] != "0")
                    {
                        FlZone zone = new FlZone();
                        zone.SetHead(SplitedLine);
                        FlZones.Add(zone);
                    }
                }
                else if (LineContent.StartsWith("(13"))//thread
                {
                    if (SplitedLine[1] != "0")
                    {
                        FlThread mt = new FlThread();
                        mt.SetHead(SplitedLine);
                        mt.Faces = new Face[mt.Index.GetCount()];
                        FlThreads.Add(mt);
                        for (int i = 0; i < mt.Faces.Length; i++)
                        {
                            ReadLine();
                            mt.Faces[i] = new Face(SplitedLine);
                        }
                    }
                }
                else if (LineContent.StartsWith("(2"))
                {
                    if (SplitedLine[1] == "2")
                    {
                        LxConsole.Error("暂不支持二维网格");
                        return false;
                    }
                }
                else if (LineContent.StartsWith("(39"))
                {
                    int ID = Convert.ToInt32(SplitedLine[1]);
                    NameMesh(FlZones, ID);
                    NameMesh(FlThreads, ID);
                }
            }
            Reader.Close();
            LineContent = null;
            SplitedLine = null;
            if (LineNum == 0)
            {
                LxConsole.Error("空文件");
                return false;
            }
            LxConsole.Info("读取网格完成。");
            return true;
        }
        protected void NameMesh<T>(List<T> meshs, int ID) where T : FlHead
        {
            foreach (var t in meshs)
            {
                if (t.ID == ID)
                {
                    t.name = SplitedLine[3];
                    break;
                }
            }
        }
    }
}
